import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'
// 一级路由
const layout = () => import('@/views/MyLayout')
const login = () => import('@/views/MyLogin')
const detail = () => import('@/views/MyDetail')
const notfound = () => import('@/views/PageOf404')
// 二级路由
const user = () => import('@/views/UserInfo')
const find = () => import('@/views/MyFind')
const question = () => import('@/views/MyQuestion')
const company = () => import('@/views/MyCompany')
const companyDetail = () => import('@/views/CompanyDetail')
const skill = () => import('@/views/InterviewSkill')
const search = () => import('@/views/SearchSkill')
const skilldetail = () => import('@/views/SkillDetail')
const share = () => import('@/views/ShareExperce')
const searchshare = () => import('@/views/SearchShare')
const sharedetail = () => import('@/views/ShareDetail')
const interview = () => import('@/views/MyInterview')
const shopdata = () => import('@/views/ShopData')

Vue.use(VueRouter)

const routes = [
  {
    name: 'layout',
    path: '/',
    component: layout,
    children: [
      { name: 'user', path: 'user', component: user },
      { name: 'find', path: 'find', component: find },
      { name: 'question', path: 'question', component: question },
      { name: 'interview', path: 'interview', component: interview },
      { name: 'company', path: 'company', component: company }
    ]
  },
  { name: 'login', path: '/login', component: login },
  { name: 'detail', path: '/detail', component: detail },
  { name: 'notfound', path: '/404', component: notfound },
  { name: 'skill', path: '/skill', component: skill },
  { name: 'search', path: '/search', component: search },
  { name: 'skilldetail', path: '/skilldetail/:id', component: skilldetail },
  { name: 'share', path: '/share', component: share },
  { name: 'searchshare', path: '/searchshare', component: searchshare },
  { name: 'sharedetail', path: '/sharedetail/:id', component: sharedetail },
  { name: 'companyDetail', path: '/companyDetail/:id', component: companyDetail },
  { name: 'shopdata', path: '/shopdata', component: shopdata },
  { path: '*', redirect: '/404' }
]

const router = new VueRouter({
  routes
})
// 全局前置守卫
const whiteList = ['/login', '/find']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path) || getToken()) {
    next()
  } else {
    Toast.fail('请先登录')
    router.push('/login')
  }
})
export default router
